package com.jihox.abookpro;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jihox.abookpro.R;
import com.jihox.abookpro.util.Code;
import com.jihox.abookpro.util.MyTextWatcher;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 接收手机号码，传给ResetPassWordActivity
 * @author york.gui
 *
 */
public class ForgotPassWordActivity extends BaseActivity{
	
	private static final int RESETPASS_CODE = 1223221;
	
	private EditText signPhoneNumber;
	private Button resetPhoneNumber;
	private EditText identifyCode;
	private Button resetIndentifyCode;
	private ImageView iv_showCode;
	private Button nextStep;
	private String real_code;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_forgot_password);
		initView();
        initHeader();
        
        //是否显示清空按钮
        signPhoneNumber.addTextChangedListener(new MyTextWatcher(resetPhoneNumber));
        identifyCode.addTextChangedListener(new MyTextWatcher(resetIndentifyCode));
        //更换验证码图片
        iv_showCode.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				iv_showCode.setImageBitmap(Code.getInstance().createBitmap());
				real_code = Code.getInstance().getCode();
			}
		});
        
        
        
        nextStep.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String customerPhoneNumber = signPhoneNumber.getText().toString().trim();               
                Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(18[0-9]))\\d{8}$");
                Matcher m = p.matcher(customerPhoneNumber);            
                if ( m.matches()) { 
                	String phoneCode = identifyCode.getText().toString();
        			if(phoneCode.equalsIgnoreCase(real_code)){	//判断验证码是否正确	
//        				Toast.makeText(ForgotPassWordActivity.this, phoneCode+"验证码正确", Toast.LENGTH_SHORT).show();
        				//跳转到VerifyActivity，传递手机号
                        Intent intent = new Intent(ForgotPassWordActivity.this, VerifyPassWordActivity.class);
                        intent.putExtra("signUpPhoneNumber", customerPhoneNumber); 
                      //跳转来源信息。来自ForgotPassWord，接下来进行重置密码
                        intent.putExtra("From", "ForgotPassWord");
//                        startActivity(intent);
                        startActivityForResult(intent, RESETPASS_CODE);
        			}else{
        				Toast.makeText(ForgotPassWordActivity.this, phoneCode+"验证码错误", Toast.LENGTH_SHORT).show();
        			}
                	
                } else {
                    Toast.makeText(ForgotPassWordActivity.this, R.string.T_Phone_Format_Error, Toast.LENGTH_SHORT).show();
                }
				
			}
		});
        
        resetPhoneNumber.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				signPhoneNumber.setText(null);				
			}
		});
        
        resetIndentifyCode.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				identifyCode.setText(null);
			}
		});
	}
	 private void initView() {        
	        signPhoneNumber = (EditText) findViewById(R.id.edit_login_phone_number);
	        resetPhoneNumber = (Button) findViewById(R.id.btn_reset_phone_number);
	        identifyCode = (EditText) findViewById(R.id.edit_identifycode);
	        resetIndentifyCode = (Button) findViewById(R.id.btn_reset_identifycode);
	        iv_showCode = (ImageView) findViewById(R.id.iv_showCode);
	        nextStep = (Button) findViewById(R.id.btn_next);
	        //将验证码用图片的形式显示出来
			iv_showCode.setImageBitmap(Code.getInstance().createBitmap());
			real_code = Code.getInstance().getCode();
			
	    }
	    /**
	     * 初始化头部
	     */
	    private void initHeader(){
	    	//头部半透明
	    	RelativeLayout rl_head = (RelativeLayout) this.findViewById(R.id.login);
	    	rl_head.setBackgroundColor(Color.argb(60, 0, 0, 0));
	    	//注册返回按钮事件
			Button backBtn = (Button) findViewById(R.id.ivBack);
			backBtn.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View view) {				
					ForgotPassWordActivity.this.finish();
				}
			});
			//设置Title文字为注册
			TextView center = (TextView) findViewById(R.id.tvTitle);
			center.setText(R.string.Forgot_Password);        	
		}
	    @Override
	    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    	// TODO Auto-generated method stub
	    	switch (requestCode) { 
	    	   case RESETPASS_CODE:
	    		   if(resultCode == RESULT_OK){
	    			   finish();
	    		   }
	    	    break;
	    	}
	    	super.onActivityResult(requestCode, resultCode, data);
	    }
}
